﻿package app.view.components.guiDima.keyboard 
{
	import app.view.events.KeyboardEventD;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	/**
	 * Клавиатура с текстовым полем
	 * @author Dmitryi_Koruagin, icq:585466579, dmitryikoruagin@mail.ru, skype: ok_ru1
	 */
	public class KeyBoardWithInputFieldDBase extends Sprite
	{
		//app.view.components.guiDima.keyboard.KeyBoardWithInputFieldDBase
		
		public var keyBoard:KeyBoardDBase;
		public var inputField:TextField;
		
		private var _currentText:String;
		
		public function KeyBoardWithInputFieldDBase() 
		{
			init();
		}
		
		private function init():void
		{
			keyBoard.visible = false;
			inputField.visible = false;
			inputField.embedFonts = true;
			inputField.text = '';
			//inputField.setSelection(0, 0);
		}
		
		/**
		 * нужно установить первоначальные параметры чтобы активировать клавиатуру
		 * @param	lang язык
		 * @param	caps капс-лок
		 * @param	text текст
		 */
		/*public function setFirstParam(lang:String=KeyBoardDBase.LANG_RUS,caps:Boolean=false,text:String=''):void
		{
			keyBoard.setFirstParam(lang, caps);
			inputField.text = text;
			
			keyBoard.visible = true;
			inputField.visible = true;
			inputField.multiline = true;
			inputField.wordWrap = true;
			inputField.maxChars = 600;
			//inputField.type = TextFieldType.INPUT;
			//inputField.setSelection(0, 0);
			
			addEventListener(KeyboardEventD.ON_INPUT_TEXT, onInputText, true);
			addEventListener(KeyboardEventD.ON_KEY_BACKSPACE, onKeyBackSpace, true);
			addEventListener(KeyboardEventD.ON_KEY_ENTER, onKeyEnter, true);
		}*/
		
		private function setEvents():void
		{
			addEventListener(KeyboardEventD.ON_INPUT_TEXT, onInputText, true);
			addEventListener(KeyboardEventD.ON_KEY_BACKSPACE, onKeyBackSpace, true);
			addEventListener(KeyboardEventD.ON_KEY_ENTER, onKeyEnter, true);
		}
		
		private function removeEvents():void
		{
			removeEventListener(KeyboardEventD.ON_INPUT_TEXT, onInputText, true);
			removeEventListener(KeyboardEventD.ON_KEY_BACKSPACE, onKeyBackSpace, true);
			removeEventListener(KeyboardEventD.ON_KEY_ENTER, onKeyEnter, true);
		}
		
		public function destroy():void
		{
			removeEvents();
			
			keyBoard.destroy();
		}
		
		/**
		 * нажали энтер
		 * @param	e
		 */
		private function onKeyEnter(e:KeyboardEventD):void 
		{
			currentText = String(inputField.text + '\n');
		}
		
		/**
		 * нажали бекспейс
		 * @param	e
		 */
		private function onKeyBackSpace(e:KeyboardEventD):void 
		{
			////trace('111',currentText,_currentText.slice());
			if (currentText!='') 
			{
				currentText = _currentText.slice(0,currentText.length-1);
			}
			
			////trace('222',currentText);
		}
		
		/**
		 * ввод текста
		 * @param	e
		 */
		private function onInputText(e:KeyboardEventD):void 
		{
			////trace('555',e.str);
			////trace('666',String(inputField.text + e.str));
			
			currentText = String(inputField.text + e.str);
			
			//inputField.text = String(inputField.text + e.str);
			//inputField.setSelection(2, 2);
			
			
		}
		
		public function get currentText():String { return inputField.text; }
		
		public function set currentText(value:String):void 
		{
			_currentText = value;
			
			inputField.text = _currentText;
		}
		
	}

}